﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint.WebControls;

namespace TRIG_MOD_STATUSSUMMARY
{
    class SPHelper
    {
        public string cmlQry { get; set; }
        public int Allcnt { get; set; }

        public int TtlCnt { get; set; }
        public int TtlDiscipCnt { get; set; }
        public int AppCnt { get; set; }
        public int ACmtsCnt { get; set; }
        public int NAppCnt { get; set; }
        public int UCNTRvCnt { get; set; }
        public int UCNSRvCnt { get; set; }
        public int UCLNRvCnt { get; set; }



        public int TtlSubmittedCnt =0;
        public int TtlApprovedCnt=0;
        public int TtlACmtsCnt=0;
        public int TtlNAppCnt=0;
        public int TtlUCNTRvCnt=0;
        public int TtlUCNSRvCnt=0;
        public int TtlUCLNRvCnt=0;



        public string loginUser { get; set; }
        public int SubmitedCnt { get; set; }

        DataTable dtResult = new DataTable();

        int cnt;

       public void GridCSS(GridView grid)
       {
            grid.Width = Unit.Percentage(100);
            grid.GridLines = GridLines.Horizontal;
            grid.HeaderStyle.CssClass = "ms-vh2";
            grid.CellPadding = 2;
            grid.BorderWidth = Unit.Pixel(5);
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
       }
        
        public void CreateTable()
        {
            dtResult = CreateDataColumns("Document Type","Total Submitted", "Approved", "Approved with Comments", "Not Approved", "CNT Review", "CNS Review", "CLN Review");
        }

        public void GetListItems(string lstName, Label lblError, string discipItem)
        {
            try
            {
        
               // Allcnt = GetAllItems(lstName);

                //SubmitedCnt=SubmitedItemsCnt(lstName,WebName);

                DocStatus("ttlDiscip", discipItem);
                TtlDiscipCnt = GetItems(lstName, cmlQry);
                string ttlDiscipcnt = CheckNull(TtlDiscipCnt);
                

                DocStatus("Approved", discipItem);
                AppCnt = GetItems(lstName, cmlQry);
                string a = CheckNull(AppCnt);

                DocStatus("Approved with Comments", discipItem);
                ACmtsCnt = GetItems(lstName, cmlQry);
                string b = CheckNull(ACmtsCnt);

                DocStatus("Not Approved", discipItem);
                NAppCnt = GetItems(lstName, cmlQry);
                string c = CheckNull(NAppCnt);

                DocStatus("UnderCNTReview", discipItem);
                UCNTRvCnt = GetItems(lstName, cmlQry);
                string d = CheckNull(UCNTRvCnt);

                DocStatus("UnderCNSReview", discipItem);
                UCNSRvCnt = GetItems(lstName, cmlQry);
                string e = CheckNull(UCNSRvCnt);

                DocStatus("UnderCLNReview", discipItem);
                UCLNRvCnt = GetItems(lstName, cmlQry);
                string f = CheckNull(UCLNRvCnt);

                string ttl = a + b + c + d + e + f;

                dtResult.Rows.Add(discipItem, TtlDiscipCnt, a, b, c, d, e, f);

                SetFooterGrid(Convert.ToInt32(ttlDiscipcnt), Convert.ToInt32(AppCnt), Convert.ToInt32(ACmtsCnt), Convert.ToInt32(NAppCnt),Convert.ToInt32(UCNTRvCnt), Convert.ToInt32(UCNSRvCnt), Convert.ToInt32(UCLNRvCnt));
               // dtResult.Rows.Add(lstName, SubmitCnt, InPrsCnt, AppCnt, RejCnt, WebName);
            }
            catch (Exception ee)
            {
               // throw;
                lblError.Text = lstName + ":" +  ee.Message;
            }
        }

        private string DocStatus(string status)
        {

            if (status == "All")
            { cmlQry = "<FieldRef Name=\"Title\" />"; }
            else if (status == "Approved" || status == "Approved with Comments" || status == "Not Approved")
            { cmlQry = "<Where><Eq><FieldRef Name=\"Approval_x0020_Status\" /><Value Type=\"Lookup\">" + status + "</Value></Eq></Where></Query>"; }
                //else if (status == "Approved with Comments")
                //{ cmlQry = "<Where><Eq><FieldRef Name=\"DocStatus\" /><Value Type=\"Lookup\">" + status + "</Value></Eq></Where>"; }
                //else if (status == "Not Approved")
                //{ cmlQry = "<Where><Eq><FieldRef Name=\"DocStatus\" /><Value Type=\"Lookup\">" + status + "</Value></Eq></Where>"; }
            else if (status == "UnderCNTReview")
            { cmlQry = "<Where><And><Contains><FieldRef Name=\"Document_x0020_Movement_x0020_Status\" /><Value Type=\"Lookup\">CNT</Value></Contains><Eq>" +
                "<FieldRef Name=\"Approval_x0020_Status\" /><Value Type=\"Lookup\">NA</Value></Eq></And></Where>"; }
            else if (status == "UnderCNSReview")
            { cmlQry = "<Where><And><Contains><FieldRef Name=\"Document_x0020_Movement_x0020_Status\" /><Value Type=\"Lookup\">CNS</Value></Contains><Eq>" +
                "<FieldRef Name=\"Approval_x0020_Status\" /><Value Type=\"Lookup\">NA</Value></Eq></And></Where>"; }
            else if (status == "UnderCLNReview")
            { cmlQry = "<Where><And><Contains><FieldRef Name=\"Document_x0020_Movement_x0020_Status\" /><Value Type=\"Lookup\">CLN</Value></Contains><Eq>" +
                "<FieldRef Name=\"Approval_x0020_Status\" /><Value Type=\"Lookup\">NA</Value></Eq></And></Where>"; }
            else
            { }

            return cmlQry;
        }


        private string DocStatus(string status,string discipiline)
        {

            if (status == "ttlDiscip")
            { cmlQry = "<Where><Eq><FieldRef Name=\"RefNoDiscipline\" /><Value Type=\"Lookup\">"+ discipiline +"</Value></Eq></Where>"; }
            else if (status == "Approved" || status == "Approved with Comments" || status == "Not Approved")
            { cmlQry = "<Where><And><Eq><FieldRef Name=\"ApprovalStatus\" /><Value Type=\"Lookup\">" + status + "</Value></Eq><Eq><FieldRef Name=\"RefNoDiscipline\" /><Value Type=\"Lookup\">" + discipiline + "</Value></Eq></And></Where>"; }
            //else if (status == "Approved with Comments")
            //{ cmlQry = "<Where><Eq><FieldRef Name=\"DocStatus\" /><Value Type=\"Lookup\">" + status + "</Value></Eq></Where>"; }
            //else if (status == "Not Approved")
            //{ cmlQry = "<Where><Eq><FieldRef Name=\"DocStatus\" /><Value Type=\"Lookup\">" + status + "</Value></Eq></Where>"; }
            else if (status == "UnderCNTReview")
            {
                cmlQry = "<Where><And><Contains><FieldRef Name=\"DocumentMomentStatus\" /><Value Type=\"Lookup\">CNT</Value></Contains><And><Eq>"+
                "<FieldRef Name=\"ApprovalStatus\" /><Value Type=\"Lookup\">NA</Value></Eq><Eq><FieldRef Name=\"RefNoDiscipline\" /><Value Type=\"Lookup\">"+discipiline +"</Value></Eq></And>"+
                "</And></Where>";
            }
            else if (status == "UnderCNSReview")
            {
                cmlQry = "<Where><And><Contains><FieldRef Name=\"DocumentMomentStatus\" /><Value Type=\"Lookup\">CNS</Value></Contains><And><Eq>" +
                "<FieldRef Name=\"ApprovalStatus\" /><Value Type=\"Lookup\">NA</Value></Eq><Eq><FieldRef Name=\"RefNoDiscipline\" /><Value Type=\"Lookup\">" + discipiline + "</Value></Eq></And>" +
                "</And></Where>";
            }
            else if (status == "UnderCLNReview")
            {
                cmlQry = "<Where><And><Contains><FieldRef Name=\"DocumentMomentStatus\" /><Value Type=\"Lookup\">CLN</Value></Contains><And><Eq>" +
                "<FieldRef Name=\"ApprovalStatus\" /><Value Type=\"Lookup\">NA</Value></Eq><Eq><FieldRef Name=\"RefNoDiscipline\" /><Value Type=\"Lookup\">" + discipiline + "</Value></Eq></And>" +
                "</And></Where>";
            }
            else
            { }

            return cmlQry;
        }


        private int SubmitedItemsCnt(string lstName, string WebName)
        {
            
            SPQuery spq = new SPQuery();
            spq.Query = "<Where><Eq><FieldRef Name=\"Author\" /><Value Type=\"User\">"+loginUser+"</Value></Eq></Where>";
            int SubCnt=0;
            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb oWeb = oSite.OpenWeb(WebName))
                {
                    SubCnt = oWeb.Lists.TryGetList(lstName).GetItems(spq).Count;
                }
            }
            return SubCnt;
        }

        protected string CheckNull(int objGrid)
        {
            if (object.Equals(objGrid, 0))
            {
                return "-";
            }
            else
            {
                return objGrid.ToString();
            }
        }  


        public void BindGrid(GridView gv)
        {
            dtResult.Rows.Add("Total", TtlSubmittedCnt, TtlApprovedCnt, TtlACmtsCnt, TtlNAppCnt, TtlUCNTRvCnt, TtlUCNSRvCnt, TtlUCLNRvCnt);
            gv.DataSource = dtResult;
            gv.DataBind();

        }

      
        public int GetAllItems(string LstTitle)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {
                        SPList oLst = oWeb.Lists.TryGetList(LstTitle);
                        SPListItemCollection oLstItmColl;
                        if (oLst != null)
                        {
                            oLstItmColl = oLst.Items;
                            cnt = oLstItmColl.Count;
                        }
                    }
                }
            });
            return cnt;
        }

        public int GetItems(string LstTitle, string camlQry)
        {
            SPQuery qry = new SPQuery();
            qry.Query = camlQry;
            
            using(SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
              using (SPWeb oWeb = oSite.OpenWeb())
              {
                
                SPList oLst = oWeb.Lists.TryGetList(LstTitle);
                SPListItemCollection oLstItmColl = oLst.GetItems(qry);
                cnt = oLstItmColl.Count;
              }
            }
            return cnt;
        }

        public static DataTable CreateDataColumns(params string[] colna)
        {
            DataTable dt = new DataTable();
            foreach (string item in colna)
            {
                dt.Columns.Add(item);
            }
            return dt;
        }

        public void GetLoginUser()
        {
            SPContext currentContext = SPContext.Current;
            loginUser = string.Empty;

            if (currentContext != null && currentContext.Web.CurrentUser != null)
            {
                SPWeb web = SPContext.Current.Web;
                loginUser = web.CurrentUser.Name;
            }
            else
            {
                loginUser = System.Web.HttpContext.Current.User.Identity.Name;
            }
        }

        public void SetFooterGrid(int s,int a,int b,int c,int d,int e,int f)
        {
            TtlSubmittedCnt = TtlSubmittedCnt+s;
            TtlApprovedCnt = TtlApprovedCnt+a;
            TtlACmtsCnt = TtlACmtsCnt+b;
            TtlNAppCnt = TtlNAppCnt+c;
            TtlUCNTRvCnt = TtlUCNTRvCnt+d;
            TtlUCNSRvCnt = TtlUCNSRvCnt+e;
            TtlUCLNRvCnt = TtlUCLNRvCnt+f;
        }

        public void g()
        {
            dtResult.Rows.Add("Total",TtlSubmittedCnt, TtlApprovedCnt, TtlACmtsCnt, TtlNAppCnt, TtlUCNTRvCnt, TtlUCNSRvCnt, TtlUCLNRvCnt);
        }

    }
}
